package com.ssyc.gsk_parents.adapter;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.github.aakira.expandablelayout.ExpandableLayout;
import com.github.aakira.expandablelayout.ExpandableLayoutListenerAdapter;
import com.github.aakira.expandablelayout.ExpandableRelativeLayout;
import com.github.aakira.expandablelayout.Utils;
import com.ssyc.common.utils.UiUtils;
import com.ssyc.common.view.MyGridView;
import com.ssyc.gsk_parents.R;
import com.ssyc.gsk_parents.bean.UnitBean;

import java.util.List;

/**
 * 家长端--教学目标--列表适配器
 * Created by Administrator on 2018/5/7 0007.
 */

public class ParentsTeacheringTargetRvAdapter extends BaseQuickAdapter<UnitBean.DataBean, BaseViewHolder> {

    private final List<UnitBean.DataBean> data;
    private Context context;
    private SparseBooleanArray expandState = new SparseBooleanArray();

    public ParentsTeacheringTargetRvAdapter(Context context, int layoutResId, @Nullable List<UnitBean.DataBean> data) {
        super(layoutResId, data);
        this.context = context;
        this.data = data;
        //设置默认的展开位置,默认第1个条目为展开位置s
        for (int i = 0; i < data.size(); i++) {
            if (i == 0) {
                expandState.append(i, true);
            } else {
                expandState.append(i, false);
            }
        }
    }

    @Override
    protected void convert(BaseViewHolder helper, UnitBean.DataBean item) {
        //单元名称
        TextView tvUnitName = helper.getView(R.id.tv_group);
        tvUnitName.setText("2-" + item.getGrade_id());
        //包含单元名称的整体相对布局
        RelativeLayout rlGroup = helper.getView(R.id.rl_group);
        ImageView iv_arrow = helper.getView(R.id.iv_arrow);

        if ("1".equals(item.getGrade_id())) {
            rlGroup.setBackgroundColor(context.getResources().getColor(R.color.green));
            tvUnitName.setTextColor(context.getResources().getColor(R.color.white));
            iv_arrow.setImageResource(R.drawable.parent_up_arrow);
        }

        //折叠布局
        ExpandableRelativeLayout expandableLayout = helper.getView(R.id.expandableLayout);
        expandableLayout.setInterpolator(item.getInterpolator());
        expandableLayout.setExpanded(expandState.get(helper.getLayoutPosition()));

        //包含切换箭头的整体布局
        RelativeLayout buttonLayout = helper.getView(R.id.button);
        expandableLayout.setListener(new ExpandableLayoutListenerAdapter() {
            @Override
            public void onPreOpen() {
//                createRotateAnimator(buttonLayout, 0f, 180f).start();
                expandState.put(helper.getLayoutPosition(), true);
                //设置背景和字体颜色
                rlGroup.setBackgroundColor(context.getResources().getColor(R.color.green));
                tvUnitName.setTextColor(context.getResources().getColor(R.color.white));

                iv_arrow.setImageResource(R.drawable.parent_up_arrow);
            }

            @Override
            public void onPreClose() {
//                createRotateAnimator(buttonLayout, 180f, 0f).start();
                expandState.put(helper.getLayoutPosition(), false);

                rlGroup.setBackgroundColor(context.getResources().getColor(R.color.white));
                tvUnitName.setTextColor(context.getResources().getColor(R.color.green));
                iv_arrow.setImageResource(R.drawable.parent_down_arrow);
            }
        });

//        buttonLayout.setRotation(expandState.get(helper.getLayoutPosition()) ? 180f : 0f);

        //包含单元名称的整体相对布局

        rlGroup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                onClickButton(expandableLayout);
            }
        });

        //展示所有课程数据的列表
        MyGridView mGv = helper.getView(R.id.mgv);

        //设置孩子的数据
        List<UnitBean.DataBean.GradeListBean> grade_list = item.getGrade_list();
        GvLessonAdapter gvAdapter = new GvLessonAdapter(context,
                grade_list, R.layout.parents_item_gv_children);
        mGv.setAdapter(gvAdapter);

        mGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
                //跳转到题型选择界面
//                UnitBean.DataBean.GradeListBean listBean = new UnitBean.DataBean.GradeListBean();
//                listBean.setGrade_id(grade_list.get(pos).getGrade_id());

                UiUtils.Toast("条目被点击了", false);
            }
        });
    }
//    public ParentsTeacheringTargetRvAdapter(Context context, int layoutResId, @Nullable List<UnitBean.DataBean> data) {
//        super(layoutResId, data);
//        this.context = context;
//        this.data = data;
//        //设置默认的展开位置,默认第1个条目为展开位置s
//        for (int i = 0; i < data.size(); i++) {
//            if (i == 0) {
//                data.get(i).setSelected(true);
//            } else {
//                data.get(i).setSelected(false);
//            }
//        }
//
//    }
//
//    @Override
//    protected void convert(BaseViewHolder helper, UnitBean.DataBean item) {
//        //单元名称
//        TextView tvUnitName = helper.getView(R.id.tv_group);
//        tvUnitName.setText("2-" + item.getGrade_id());
//
//
//        //箭头图片
//        ImageView iv = helper.getView(R.id.lv);
//
//        //展示所有课程数据的列表
//        MyGridView mGv = helper.getView(R.id.mgv);
//
//        //设置孩子的数据
//        List<UnitBean.DataBean.GradeListBean> grade_list = item.getGrade_list();
//        GvLessonAdapter gvAdapter = new GvLessonAdapter(context, grade_list, R.layout.parents_item_gv_children);
//        mGv.setAdapter(gvAdapter);
//
//        mGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
////                //跳转到题型选择界面
////                Intent intent = new Intent(context, StudentChooseQuestionTypeActivity.class);
////
////                //将当前对象传递过去
////                intent.putExtra(StudentChooseQuestionTypeActivity.LESSONDATA, grade_list.get(pos));
////
////                context.startActivity(intent);
//                UiUtils.Toast("条目被点击了", false);
//            }
//        });
//
//        //包含单元名称的整体相对布局
//        RelativeLayout rlGroup = helper.getView(R.id.rl_group);
//        rlGroup.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(final View v) {
//                item.setSelected(!item.isSelected());
//                notifyDataSetChanged();
//            }
//        });
//
//
//        //根据选中状态确定各状态值
//        if (item.isSelected()) {
//            //选中状态
//            rlGroup.setBackgroundColor(context.getResources().getColor(R.color.parent_open));
//            tvUnitName.setTextColor(context.getResources().getColor(R.color.parent_close));
//            iv.setImageResource(R.drawable.parent_up_arrow);
//            mGv.setVisibility(View.VISIBLE);
//        } else {
//            rlGroup.setBackgroundColor(context.getResources().getColor(R.color.parent_close));
//            tvUnitName.setTextColor(context.getResources().getColor(R.color.parent_open));
//            iv.setImageResource(R.drawable.parent_down_arrow);
//            mGv.setVisibility(View.GONE);
//        }
//    }

    /**
     * 创建动画的方法
     *
     * @param target
     * @param from
     * @param to
     * @return
     */
    public ObjectAnimator createRotateAnimator(final View target, final float from, final float to) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(target, "rotation", from, to);
        animator.setDuration(300);
        animator.setInterpolator(Utils.createInterpolator(Utils.LINEAR_INTERPOLATOR));
        return animator;
    }

    /**
     * 折叠布局的切换方法
     *
     * @param expandableLayout
     */
    private void onClickButton(final ExpandableLayout expandableLayout) {
        expandableLayout.toggle();
    }
}